﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TEKADR
    {
        public TEKADR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VEKADR();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            ORDERNUM = "";
            ORDERNUM = KRS + LFD + POS + KUNDNR + "B";
            base.QueryCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.UpdateEKADR(dwMaster, app.company, 0);
            base.SaveCommand(sender, e);
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请先执行查询");
                return;
            }
            base.EditCommand(sender, e);
        }

        public override void LOADBYAUF()
        {
            if (ORDERNUM.Length < 19)
                return;
            TBMain.Clear();
            KRS = ORDERNUM.Substring(1, 2);
            LFD = ORDERNUM.Substring(3, 6);
            POS = ORDERNUM.Substring(9, 3);
            if (!IsLoaded)
                return;
            dwData = TBMain.Clone().NewRow();
            dwData["EKAMANDANT"] = app.Mandant;
            dwData["EKAAUFKRS"] = KRS;
            dwData["EKAAUFLFD"] = LFD;
            dwData["EKAAUFPOS"] = POS;
            SResult = TMV.QEKA01(dwData);
            MergeData(SResult, true, true);
            base.LOADBYAUF();
        }

        private void EKADRCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (dwKDR == null)
            {
                MessageBox.Show("请选择一个客户地址");
                return;
            }
            TMV.EKABNAME1 = dwKDR["KDANAME1"].ToString();
            TMV.EKABNAME2 = dwKDR["KDANAME2"].ToString();
            TMV.EKABSTRASSE1 = dwKDR["KDASTRASSE1"].ToString();
            TMV.EKABSTRASSE2 = dwKDR["KDASTRASSE2"].ToString();
            TMV.EKABLKZ = dwKDR["KDALAND"].ToString();
            TMV.EKABPLZ = dwKDR["KDAPLZ"].ToString(); 
            TMV.EKABORT = Helper.Left((dwKDR["KDATTEX"].ToString().Trim() + ":" + dwKDR["KDATEL"].ToString().Trim()).PadRight(40), 40);
            TMV.EKABPOSTF = Helper.Left(dwKDR["KDAPOSTFACH"].ToString().PadRight(15), 15);
            TMV.EKAANZ2 = "01";
            TMV.EKALIEFNRK12 = dwKDR["KDAKUNDNR"].ToString();
            TMV.EKAADRSLK12 = dwKDR["KDAADRSL"].ToString(); 
            TMV.EKAANZK12 = "01";
            if (dwMaster == null)
                return;
        }

        private void VVADRCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (dwKDR == null)
                return;
            TMV.EKALNAME1 = dwKDR["KDANAME1"].ToString();
            TMV.EKALNAME2 = dwKDR["KDANAME2"].ToString();
            TMV.EKALSTRASSE1 = dwKDR["KDASTRASSE1"].ToString();
            TMV.VKALSTRASSE2 = dwKDR["KDASTRASSE2"].ToString();
            TMV.EKALLKZ = dwKDR["KDALAND"].ToString();
            TMV.EKALPLZ = dwKDR["KDAPLZ"].ToString();
            TMV.EKALORT =Helper.Left( (dwKDR["KDATTEX"].ToString().Trim()+":"+dwKDR["KDATEL"].ToString().Trim()).PadRight(40),40); 
            TMV.EKALBZBEZ = Helper.Left(dwKDR["KDAPOSTFACH"].ToString().PadRight(13), 13);
             TMV.EKAANZ1 = "01".ToString();
            TMV.EKALIEFNRK11 = dwKDR["KDAKUNDNR"].ToString();
            TMV.EKAADRSLK11 = dwKDR["KDAADRSL"].ToString();
             TMV.EKAANZK11 = "01".ToString();
            if (dwMain == null)
                return;

        }

        private void VRADRCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (dwKDR == null)
                return;
            TMV.EKAADRSL3 = dwKDR["KDAADRSL"].ToString();
            TMV.EKALIEFNR3 = dwKDR["KDAKUNDNR"].ToString();
            TMV.EKAANZK22 = "00";
            TMV.EKAANZK13 = "00";
            if (dwMaster == null)
                return;
        }

        public override void OnVVTChg(string vvt)
        {
            ORDERNUM = "";
            ORDERNUM = vvt;
            base.OnVVTChg(vvt);
        }
        public override void OnVKPChg(string value)
        {
            ORDERNUM = "";
            ORDERNUM = value;
            base.OnVKPChg(value);
        }

    }
}
